/* GUI CONVERTED from apvocsua.p (converter v1.76) Tue Mar 18 16:12:39 2003 */
/* apvocsua.p - AP VOUCHER INVENTORY COST UPDATE ON INVENTORY DATABASE        */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.18 $                                           */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 9.0           CREATED: 03/15/99   BY: *M0BG* Jeff Wootton        */
/* REVISION: 9.1          MODIFIED: 10/01/99   BY: *N014* Jeff Wootton        */
/* REVISION: 9.1          MODIFIED: 11/01/99   BY: *N053* Jeff Wootton        */
/* REVISION: 9.1     LAST MODIFIED: 12/27/99   BY: *J3MJ* Rajesh Lokre        */
/* REVISION: 9.1     LAST MODIFIED: 08/11/00   BY: *N0KK* jyn                 */
/* Revision: 1.7     BY: Santosh Rao           DATE: 02/14/02  ECO: *N190*    */
/* Revision: 1.8     BY: Rajiv Ramaiah         DATE: 03/20/02  ECO: *M1WW*    */
/* Revision: 1.9     BY: Samir Bavkar          DATE: 04/05/02  ECO: *P000*    */
/* Revision: 1.10    BY: Patrick Rowan         DATE: 04/29/02  ECO: *P05Q*    */
/* Revision: 1.13    BY: Manjusha Inglay       DATE: 07/29/02  ECO: *N1P4*    */
/* Revision: 1.14    BY: Samir Bavkar          DATE: 08/16/02  ECO: *P0FM*    */
/* Revision: 1.15    BY: Jose Alex        DATE: 08/19/02  ECO: *M1XR*   */
/* $Revision: 1.18 $      BY: Tiziana Giustozzi   DATE: 02/20/03 ECO: *P0MX* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/************************************************************************/
/* This program is run from the following program:                      */
/*  apvocsu1.p  AP Voucher Maintenance, Cost Updates on AP database     */
/************************************************************************/

/*! --------------------------------------------------------------------------*
 *                      COST UPDATE RESULTS AS OF ECO M0BG
 *                      ==================================
 *
 * 4 Costing Scenarios:
 *       1                 2                 3                 4
 * TAX   NORMAL PO         NORMAL PO         SUBCONTRACT PO    SUBCONTRACT  PO
 * ---   ----------------  ----------------  ----------------  ----------------
 *       GL    CURR  CURR  GL    CURR  CURR  GL    CURR  CURR  GL    CURR  CURR
 *       AVG   AVG   LAST  STD   AVG   LAST  AVG   AVG   LAST  STD   AVG   LAST
 *       ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 *                                                UPDATES
 *                                              WO WIP COST
 *                                              -----------
 * PO RECEIPT at 10.00 + 1.00 tax
 *
 * US    11.00 11.00 11.00       11.00 11.00       10.00             11.00 11.00
 * GTM   11.00 11.00 11.00       11.00 11.00       11.00             11.00 11.00
 *
 *
 * VOUCHER at 12.00 + 1.20 tax (2.00 + 0.20 RATE VARIANCE)
 *       SELECT "INVENTORY"                     SELECT "WIP"
 *
 * US    13.20 13.20 12.00       13.00 12.00       12.00             13.00 12.00
 * GTM   13.20 13.20 12.00       13.00 13.20       13.00             13.00 12.00
 *                                                               #6> 12.00
 *
 * VOUCHER DELETION (OR REVERSAL)
 *
 * US    11.00 11.00 11.00       11.00 12.00       10.00             12.00 12.00
 * GTM   11.00 11.00 12.00       11.00 13.20       11.00             11.00 11.00
 *
 *
 * NOTES:
 *  1. PO RECEIPT adds tax to all costs, except US-TAX to WIP
 *  2. VOUCHER updates (and reverses) CURR-LAST inconsistently
 *  3. In general, VOUCHER adds tax for NORMAL PO only (but inconsistent)
 *  4. ERS always sets "Inventory" for Normal PO, GL-AVG
 *  5. ERS never sets "WIP" for Subcontract PO, GL-AVG
 *  6. In the 2 GL-STD cases, if QOH is zero,
 *     CURR-AVG is set to voucher cost (without tax)
 *     (In the Subcontract GL-STD case, Receipt does not update QOH)
 *----------------------------------------------------------------------------*
 */

/*! --------------------------------------------------------------------------*
 *                      "VPH_HIST" FIELD USAGE AS OF 10/20/99
 *                      =====================================
 *
 *                                     4 Costing scenarios: 1   2    3   4
 * FIELD            USAGE                                   NORMAL   SUBCONTRACT
 * -----            -----                                   GL  GL   GL  GL
 *                                                          AVG STD  AVG STD
 *                                                          --- ---  --- ---
 * vph_adj_inv      Variance applied to Inventory           y
 * vph_adj_wip      Subcontract Variance applied to WIP              y
 * vph_adj_amt      Cost adjustment amount                  y   y    y   y
 * vph_dscr_amt     Cost adjustment discrepancy amount      y
 * vph_cf_adj_amt   Confirmed cost adj amount               y   y    y   y
 * vph_cf_dscr_amt  Confirmed cost adj discrepancy amount   y
 * vph_adj_prv_cst  Previous cost                               y        y
 * vph_qoh_at_adj   QOH at time of adjustment               y   y    y   y
 * vph_costadj_acct Inventory account                       y
 *                  WIP account                                      y
 * vph_costadj_sub  Inventory sub-account                   y
 *                  WIP sub-account                                  y
 * vph_costadj_cc   Inventory cost center                   y
 *                  WIP cost center                                  y
 * vph_dscr_acct    Inventory discrepancy account           y        y
 * vph_dscr_sub     Inventory discrepancy sub-account       y        y
 * vph_dscr_cc      Inventory discrepancy cost center       y        y
 * vph_project      Project (to override pvo_project)       Regardless
 *
 *----------------------------------------------------------------------------*
*/

/* DEFINED TWO NEW LOCAL VARIABLES l_old_value1 AND l_new_value1   */
/* TO STORE UNROUNDED VALUES OF OLD COST AND NEW COST RESPECTIVELY */
/* AND THE DIFFERENCE IS STORED IN p_vph_cf_adj_amt WHICH IS AN    */
/* UNROUNDED VALUE. THIS IS DONE TO REVERT BACK THE COST TO ITS    */
/* ORIGINAL COST WHEN THE VOUCHER IS DELETED.                      */

{mfdeclre.i}

{gldydef.i new} /* DEFINE NEW INSTANCE ON DAYBOOKS VARIABLES */
{gldynrm.i new} /* DEFINE NEW INSTANCE OF NRM FOR THIS DATABASE */

define input        parameter p_ap_database   like si_db.
define input        parameter p_ap_ref        like ap_ref.
define input        parameter p_ap_acct       like ap_acct.
define input        parameter p_ap_sub        like ap_sub.
define input        parameter p_ap_cc         like ap_cc.
define input        parameter p_vo_ref        like vo_ref.
define input        parameter p_vo_curr       like vo_curr.
define input        parameter p_vo_ex_rate    like vo_ex_rate.
define input        parameter p_vo_ex_rate2   like vo_ex_rate2.
define input        parameter p_pod_wo_lot    like pod_wo_lot.
define input        parameter p_pod_op        like pod_op.
define input        parameter p_pvo_part      like pvo_part.
define input        parameter p-po-site       like pod_site.
define input        parameter p_pvo_shipto    like pvo_shipto.
define input        parameter p-receipt-type  like pod_type.
define input        parameter p_pvo_cost_update like pvo_cost_update.
define input        parameter p-um-conv       like pod_um_conv.
define input        parameter p_pvo_trans_qty like pvo_trans_qty.
define input        parameter p_pvo_order     like pvo_order.
define input        parameter p_pvo_line      like pvo_line.
define input        parameter p_pvo_internal_ref like pvo_internal_ref.
define input        parameter p_pvo_project   like pvo_project.
define input        parameter p-po-pur-cost   like pod_pur_cost.
define input        parameter p_vph_adj_inv   like vph_adj_inv.
define input        parameter p_vph_inv_cost  like vph_inv_cost.
define input        parameter p_vph_inv_qty   like vph_inv_qty.
define input        parameter p_vph_curr_amt  like vph_curr_amt.
define input        parameter p_rndmthd       as character.
define input        parameter p_rate_var_amt     as decimal.
define input        parameter p_curr_cost_taxamt as decimal.
define input        parameter p_taxamt           as decimal.
define input        parameter p_tax_on_receipt   as decimal.
define input        parameter p_tax_on_vph_price as decimal.
define input-output parameter p_totl_amt         as decimal.
define input-output parameter p_unit_amt         as decimal.

define input-output parameter p_vph_costadj_acct like vph_costadj_acct.
define input-output parameter p_vph_costadj_sub  like vph_costadj_sub.
define input-output parameter p_vph_costadj_cc   like vph_costadj_cc.
define input-output parameter p_vph_dscr_acct    like vph_dscr_acct.
define input-output parameter p_vph_dscr_sub     like vph_dscr_sub.
define input-output parameter p_vph_dscr_cc      like vph_dscr_cc.

define input-output parameter p_vph_adj_amt      like vph_adj_amt.
define input-output parameter p_vph_adj_prv_cst  like vph_adj_prv_cst.
define input-output parameter p_vph_qoh_at_adj   like vph_qoh_at_adj.
define input-output parameter p_vph_dscr_amt     like vph_dscr_amt.
define input-output parameter p_vph_cf_adj_amt   like vph_cf_adj_amt.
define input-output parameter p_vph_cf_dscr_amt  like vph_cf_dscr_amt.

define input-output parameter p_vph_adj_wip      like vph_adj_wip.

define variable glx_mthd         like cs_method.
define variable glx_set          like cs_set.
define variable cur_mthd         like cs_method.
define variable cur_set          like cs_set.
define variable qty_oh           like in_qty_oh.
define variable qty_po           like pvo_trans_qty.
define variable pct_po           as decimal.
define variable oldcst           like sct_cst_tot.
define variable curflag          like mfc_logical.
define variable glxflag          like mfc_logical.
define variable gl_tmp_amt       as decimal.
define variable l_new_value      like vod_amt           no-undo.
define variable l_old_value      like vod_amt           no-undo.
define variable l_gl_ref         like glt_ref           no-undo.
define variable mc-error-number  like msg_nbr           no-undo.

define variable l_new_value1      like vod_amt          no-undo.
define variable l_old_value1      like vod_amt          no-undo.
define variable l_vph_cf_adj_amt  like vph_cf_adj_amt   no-undo.
define variable l_vph_cf_dscr_amt like vph_cf_dscr_amt  no-undo.

define new shared variable ref         like glt_ref.
define new shared variable eff_date    like glt_effdate
   initial today.
define new shared variable transtype   as character.

for first in_mstr
   fields(in_qty_oh in_gl_cost_site in_gl_set
   in_qty_nonet)
   where in_part = p_pvo_part
     and in_site = p_pvo_shipto
   no-lock:
end. /* FOR FIRST in_mstr */

if not available in_mstr
then
   return.

for first gl_ctrl
   fields(gl_rnd_mthd)
   no-lock:
end. /* FOR FIRST gl_ctrl */

for first icc_ctrl
   fields(icc_cur_ap)
   no-lock:
end. /* FOR FIRST icc_ctrl */

assign
   qty_oh  = in_qty_oh + in_qty_nonet
   curflag = no
   glxflag = no.

/*DETERMINE COSTING METHOD*/
{gprun.i ""csavg01.p""
   "(input p_pvo_part,
     input p_pvo_shipto,
     output glx_set,
     output glx_mthd,
     output cur_set,
     output cur_mthd
     )"}
/*GUI*/ if global-beam-me-up then undo, leave.


/*POST TO VARIANCE - CURRENT INVENTORY COST*/

if p-receipt-type = ""
   and p_pvo_cost_update   /* PO, UPDATE AVG/LAST COST = YES */
   and icc_cur_ap   /* IC CONTROL, CURRENT COST FROM AP = YES */
   and (cur_mthd = "AVG" or cur_mthd = "LAST")
   and (glx_mthd <> "AVG")
then do:
   {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}

   assign
      curflag = yes
      curcst  = sct_mtl_tl.

   if cur_mthd = "LAST"
   or qty_oh   <= 0
   then
      assign
         /* SAVE PREVIOUS COST */
         p_vph_adj_prv_cst = sct_mtl_tl
         /* INCLUDE NON-RECOVERABLE TAX IN MATERIAL COST */
         sct_mtl_tl = (p_vph_inv_cost / p-um-conv)
                    + p_curr_cost_taxamt.
   else if cur_mthd = "AVG"
   then do:
      assign
         pct_po = 0
         qty_po = p_pvo_trans_qty * p-um-conv.

      /* WHEN qty_po < 0  ( PO RETURNS/ NEGATIVE RECEIPTS)      */
      /* pct_po WAS NEGATIVE LEADING TO INCORRECT CURRENT COST. */
      /* pct_po SHOULD HAVE A POSITIVE VALUE.                   */

      if qty_po > 0
      then
         pct_po = min(1,qty_oh / qty_po).
      else if qty_po < 0
      then
         pct_po = min(1,( - 1 * (qty_oh / qty_po))).

      /*sct_mtl_tl = ((sct_mtl_tl * qty_oh) + (pct_po * p_totl_amt)) / qty_oh.  modify by tony*/
        sct_mtl_tl = ((sct_mtl_tl * qty_oh) + (pct_po * (p_totl_amt + p_taxamt))) / qty_oh.
	    if  sct_mtl_tl < curcst
            and sct_mtl_tl < p-po-pur-cost
            and sct_mtl_tl < (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_mtl_tl = min(curcst,p-po-pur-cost)
                  sct_mtl_tl = min(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).
            else
            if  sct_mtl_tl > curcst
            and sct_mtl_tl > p-po-pur-cost
            and sct_mtl_tl > (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_mtl_tl = max(curcst,p-po-pur-cost)
                  sct_mtl_tl = max(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).

   end. /* ELSE IF cur_mthd = "AVG" */

   if sct_mtl_tl <= 0
   then
      sct_mtl_tl = curcst.

   if sct_mtl_tl <> curcst
   then
      assign
         sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                     + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                     + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                     + sct_sub_ll
         sct_cst_date = today.

   assign
      /* STORE QOH AS OF THIS COST ADJUSTMENT */
      p_vph_qoh_at_adj = qty_oh
      /* STORE COST ADJUSTMENT AMOUNT */
      p_vph_adj_amt = p_totl_amt
      /* STORE COST ADJUSTMENT DISCREPANCY AMOUNT */
      p_vph_dscr_amt = 0
      /* STORE CONFIRMED COST ADJUSTMENT AMOUNT */
      p_vph_cf_adj_amt = p_totl_amt
      /* STORE CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT */
      p_vph_cf_dscr_amt = 0.
end. /* IF p-receipt-type = "" AND p_pvo_cost_update */

/*POST TO VARIANCE - CURRENT SUBCONTRACT COST*/

else if p-receipt-type = "S"
   and p_pvo_cost_update   /* PO, UPDATE AVG/LAST COST = YES */
   and icc_cur_ap   /* IC CONTROL, CURRENT COST FROM AP = YES */
   and (cur_mthd = "AVG" or cur_mthd = "LAST")
   and (glx_mthd <> "AVG")
then do:
   for first wr_route
      fields /* (none) */
      where wr_lot = p_pod_wo_lot
        and wr_op  = p_pod_op
      no-lock:
   end. /* FOR FIRST wr_route */

   if not available wr_route
   then do:
      {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}

      assign
         curflag = yes
         curcst  = sct_sub_tl.

      if cur_mthd = "LAST"
      or qty_oh   <= 0
      then
         assign
            /* SAVE PREVIOUS COST */
            p_vph_adj_prv_cst = sct_sub_tl
            sct_sub_tl        = p_vph_inv_cost / p-um-conv.
      else if cur_mthd = "AVG"
      then do:
         assign
            pct_po = 0
            qty_po = p_pvo_trans_qty * p-um-conv.

         if qty_po <> 0
         then
            pct_po = min(1,qty_oh / qty_po).

         /*sct_sub_tl = ((sct_sub_tl * qty_oh) + (pct_po * p_totl_amt)) / qty_oh. modify by tony */
          sct_sub_tl = ((sct_sub_tl * qty_oh) + (pct_po * (p_totl_amt + p_taxamt))) / qty_oh. 
	    if  sct_sub_tl < curcst
            and sct_sub_tl < p-po-pur-cost
            and sct_sub_tl < (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_sub_tl = min(curcst,p-po-pur-cost)
                  sct_sub_tl = min(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).
            else
            if  sct_sub_tl > curcst
            and sct_sub_tl > p-po-pur-cost
            and sct_sub_tl > (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_sub_tl = max(curcst,p-po-pur-cost)
                  sct_sub_tl = max(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).

        

      end. /* ELSE IF cur_mthd = "AVG" */

      if sct_sub_tl <= 0
      then
         sct_sub_tl = curcst.

      if sct_sub_tl <> curcst
      then
         assign
            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                        + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                        + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                        + sct_sub_ll
            sct_cst_date = today.
   end. /* IF NOT AVAILABLE wr_route */

   assign
      /* STORE QOH AS OF THIS COST ADJUSTMENT */
      p_vph_qoh_at_adj = qty_oh
      /* STORE COST ADJUSTMENT AMOUNT */
      p_vph_adj_amt = p_totl_amt
      /* STORE COST ADJUSTMENT DISCREPANCY AMOUNT */
      p_vph_dscr_amt = 0
      /* STORE CONFIRMED COST ADJUSTMENT AMOUNT */
      p_vph_cf_adj_amt = p_totl_amt
      /* STORE CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT */
      p_vph_cf_dscr_amt = 0.
end. /* ELSE IF p-receipt-type = "S" AND p_pvo_cost_update */

/*POST TO WIP - GL SUBCONTRACT COST*/

else if p-receipt-type = "S"
   and (glx_mthd = "AVG")
then do:

   if p_vph_adj_wip
   then do:
      for first wo_mstr
         where wo_lot = p_pod_wo_lot
         exclusive-lock:

         if not wo_acct_close
         then
            for first wr_route
               where wr_lot = p_pod_wo_lot
                 and wr_op  = p_pod_op
               exclusive-lock:

               assign
                  wr_sub_act = wr_sub_act + p_totl_amt
                  wo_wip_tot = wo_wip_tot + p_totl_amt
                  wo_sub_tot = wo_sub_tot + p_totl_amt
                  /* STORE QOH AS OF THIS COST ADJUSTMENT */
                  p_vph_qoh_at_adj = qty_oh
                  /* STORE COST ADJUSTMENT AMOUNT */
                  p_vph_adj_amt = p_totl_amt
                  /* STORE COST ADJUSTMENT DISCREPANCY AMOUNT */
                  p_vph_dscr_amt = 0
                  /* STORE CONFIRMED COST ADJUSTMENT AMOUNT */
                  p_vph_cf_adj_amt = p_totl_amt
                  /* STORE CONFIRMED COST ADJUSTMENT */
                  /* DISCREPANCY AMOUNT */
                  p_vph_cf_dscr_amt = 0
                  p_vph_costadj_acct = wo_acct
                  p_vph_costadj_sub = wo_sub
                  p_vph_costadj_cc = wo_cc.

               /* GET PRODUCT LINE DISCREPANCY ACCOUNT */
               run get_inv_and_discr_accts
                  (input p_pvo_part,
                   input p_pvo_shipto,
                   input false, /* DO NOT WANT INVENTORY ACCOUNT */
                   input true, /* WANT DISCREPANCY ACCOUNT */
                   input-output p_vph_costadj_acct,
                   input-output p_vph_costadj_sub,
                   input-output p_vph_costadj_cc,
                   input-output p_vph_dscr_acct,
                   input-output p_vph_dscr_sub,
                   input-output p_vph_dscr_cc).
            end. /* FOR FIRST wr_route */
      end. /* FOR FIRST wo_mstr */
      if not available wo_mstr
      then
         p_vph_adj_wip = false.
   end. /* IF p_vph_adj_wip */
   else
      /* VARIANCE IS NOT TO BE APPLIED TO WIP, */
      /* ZERO THE VARIANCE AND DISCREPANCY AMOUNTS. */
      assign
         /* STORE ZERO COST ADJUSTMENT AMOUNT */
         p_vph_adj_amt = 0
         /* STORE ZERO COST ADJUSTMENT DISCREPANCY AMOUNT */
         p_vph_dscr_amt = 0
         /* STORE ZERO CONFIRMED COST ADJUSTMENT AMOUNT */
         p_vph_cf_adj_amt = 0
         /* STORE ZERO CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT */
         p_vph_cf_dscr_amt = 0
         p_totl_amt = 0.
end. /* ELSE IF p-receipt-type = "S" */

/*POST TO INVENTORY - CURRENT & GL INVENTORY COST*/
else
if p-receipt-type    = ""
   and (glx_mthd = "AVG")
then do:

   assign
      /* ADD NON-RECOVERABLE TAX TO VARIANCE (TOTL_AMT) */
      p_totl_amt = p_totl_amt + p_taxamt
      /* CALCULATE PO QTY */
      qty_po = p_pvo_trans_qty * p-um-conv.

   /* IF VARIANCE IS NOT TO BE APPLIED TO INVENTORY, */
   /* ZERO THE VARIANCE AND THE NON-RECOVERABLE TAX. */

   if not p_vph_adj_inv
      or qty_po < 0
   then
      assign
         p_vph_adj_amt  = 0
         p_vph_dscr_amt = 0
         p_totl_amt     = 0
         p_taxamt       = 0.

   assign
      p_unit_amt = (p_tax_on_receipt + p_totl_amt)
                    / (p_vph_inv_qty * p-um-conv)
      pct_po     = 0.

   if qty_po > 0
   then
      pct_po = min(1,qty_oh / qty_po).

   /* ENSURE COST ADJUSTMENT FIELDS ARE POPULATED            */
   /* REGARDLESS OF WHETHER COST ADJUSTMENT IS NECESSARY AND */
   /* REGARDLESS OF WHETHER QTY_OH > 0                       */
   /* BECAUSE FIELDS ARE USED IN OTHER PROGRAMS (apvomtk1.p) */

   assign
      /* STORE CONFIRMED COST ADJUSTMENT AMOUNT */
      p_vph_cf_adj_amt = (pct_po * (p_totl_amt + p_tax_on_receipt))

      /* STORE CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT */
      p_vph_cf_dscr_amt = p_totl_amt + p_tax_on_receipt - p_vph_cf_adj_amt.

   /* FOR VOUCHER CREATED UNCONFIRMED BEFORE ECO M0BG, */
   /* POPULATE ADJUSTMENT AMOUNT. */

   if p_vph_adj_amt      = 0
      and p_vph_dscr_amt = 0
   then
      p_vph_adj_amt = p_totl_amt + p_tax_on_receipt.

   /* SAVE QOH AS OF THIS COST ADJUSTMENT */
   p_vph_qoh_at_adj = qty_oh.

   /* GET PRODUCT LINE INVENTORY AND DISCREPANCY ACCOUNTS */
   run get_inv_and_discr_accts
      (input p_pvo_part,
      input p_pvo_shipto,
      input true, /* WANT INVENTORY ACCOUNT */
      input true, /* WANT DISCREPANCY ACCOUNT */
      input-output p_vph_costadj_acct,
      input-output p_vph_costadj_sub,
      input-output p_vph_costadj_cc,
      input-output p_vph_dscr_acct,
      input-output p_vph_dscr_sub,
      input-output p_vph_dscr_cc).

   if  qty_oh > 0
   and pct_po > 0
   then do:
      {gpsct01.i &set=glx_set &part=p_pvo_part &site=p_pvo_shipto}

      assign
         glxflag      = yes
         glxcst       = sct_mtl_tl
         oldcst       = sct_cst_tot
         sct_mtl_tl   = ((sct_mtl_tl * qty_oh)
                      + (pct_po * (p_totl_amt + p_tax_on_receipt))) / qty_oh

         /* l_old_value & l_old_value1 SHOULD BE CALCULATED */
         /* BASED ON MATERIAL COST BUT NOT ON TOTAL COST    */
         l_old_value1 = glxcst * qty_oh
         l_old_value  = glxcst * qty_oh.

      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output l_old_value,
           input gl_rnd_mthd,
           output mc-error-number)"}
      if mc-error-number <> 0
      then do:
         {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
      end. /* IF mc-error-number <> 0 */

      /* CHECK THAT NEW AVERAGE COST DOES NOT GO LOWER */
      /* THAN ANY OF THE INPUT VALUES */
      if  sct_mtl_tl < glxcst
      and sct_mtl_tl < p-po-pur-cost
      and sct_mtl_tl < (p_vph_inv_cost + p_tax_on_vph_price) / p-um-conv
      then do:

         if qty_po > 0
         then
            /* DO RECEIPTS, NOT RETURNS */
            assign
               sct_mtl_tl = min(glxcst,p-po-pur-cost)
               sct_mtl_tl = min(sct_mtl_tl,
                                (p_vph_inv_cost + p_tax_on_vph_price)
                                / p-um-conv).
      end. /* IF sct_mtl_tl < glxcst AND .. */
      else
         /* CHECK THAT NEW AVERAGE COST DOES NOT GO HIGHER */
         /* THAN ANY OF THE INPUT VALUES */
      if sct_mtl_tl  > glxcst
      and sct_mtl_tl > p-po-pur-cost
      and sct_mtl_tl > (p_vph_inv_cost + p_tax_on_vph_price) / p-um-conv
      then
         assign
            sct_mtl_tl = max(glxcst,p-po-pur-cost)
            sct_mtl_tl = max(sct_mtl_tl, (p_vph_inv_cost + p_tax_on_vph_price)
                         / p-um-conv).

      if sct_mtl_tl <= 0
      then
         sct_mtl_tl = glxcst.

      assign
         l_new_value1 = sct_mtl_tl * qty_oh
         l_new_value  = sct_mtl_tl * qty_oh.

      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output l_new_value,
           input gl_rnd_mthd,
           output mc-error-number)"}
      if mc-error-number <> 0
      then do:
         {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
      end. /* IF mc-error-number <> 0 */

      assign
         /* STORE CONFIRMED COST ADJUSTMENT AMOUNT */
         p_vph_cf_adj_amt = l_new_value1 - l_old_value1
         l_vph_cf_adj_amt = l_new_value  - l_old_value
         /* STORE CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT */
         p_vph_cf_dscr_amt = p_totl_amt + p_tax_on_receipt - p_vph_cf_adj_amt.

      if sct_mtl_tl <> glxcst
      then do:
         assign
            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                        + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                        + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                        + sct_sub_ll
            sct_cst_date = today.

         {ictrans.i
            &addrid=""""
            &bdnstd=0
            &cracct=p_ap_acct
            &crsub=p_ap_sub
            &crcc=p_ap_cc
            &crproj=p_pvo_project
            &curr=""""
            &dracct=p_vph_costadj_acct
            &drsub=p_vph_costadj_sub
            &drcc=p_vph_costadj_cc
            &drproj=p_pvo_project
            &effdate=eff_date
            &exrate=0
            &exrate2=0
            &exratetype=""""
            &exruseq=0
            &glamt=0
            &lbrstd=0
            &line=0
            &location=""""
            &lotnumber=""""
            &lotref=""""
            &lotserial=""""
            &mtlstd=0
            &ordernbr=p_pvo_order
            &ovhstd=0
            &part=p_pvo_part
            &perfdate=?
            &price="sct_cst_tot - oldcst"
            &quantityreq=qty_po
            &quantityshort=0
            &quantity=0
            &revision=""""
            &rmks=""""
            &shiptype=""""
            &site=p_pvo_shipto
            &slspsn1=""""
            &slspsn2=""""
            &sojob=""""
            &substd=0
            &transtype=""CST-ADJ""
            &msg=5207
            &ref_site=tr_site
            }

         assign
            tr_gl_amt    = l_vph_cf_adj_amt
            trgl_gl_amt  = l_vph_cf_adj_amt
            trgl_gl_ref  = p_ap_ref
            trgl_type    = "RCT-AVG"
            trgl_dy_code = dft-daybook
            trgl_dy_num  = nrm-seq-num.

         /*UPDATE CURRENT COST*/
         {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
         assign
            curflag = yes
            curcst  = sct_mtl_tl.

         if cur_mthd = "LAST"
         then
            sct_mtl_tl = p_vph_inv_cost / p-um-conv.
         else
         if cur_mthd = "AVG"
         then do:
            sct_mtl_tl = ((sct_mtl_tl * qty_oh)
                         + (pct_po * (p_totl_amt + p_tax_on_receipt))) / qty_oh.

            /* CHECK THAT NEW AVERAGE COST DOES NOT GO LOWER */
            /* THAN ANY OF THE INPUT VALUES */
            if  sct_mtl_tl < curcst
            and sct_mtl_tl < p-po-pur-cost
            and sct_mtl_tl < (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_mtl_tl = min(curcst,p-po-pur-cost)
                  sct_mtl_tl = min(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).
            else
            /* CHECK THAT NEW AVERAGE COST DOES NOT GO HIGHER */
            /* THAN ANY OF THE INPUT VALUES */
            if  sct_mtl_tl > curcst
            and sct_mtl_tl > p-po-pur-cost
            and sct_mtl_tl > (p_vph_curr_amt + p_tax_on_vph_price)
                             / p-um-conv
            then
               assign
                  sct_mtl_tl = max(curcst,p-po-pur-cost)
                  sct_mtl_tl = max(sct_mtl_tl,
                                  (p_vph_curr_amt + p_tax_on_vph_price)
                                  / p-um-conv).
         end.  /* ELSE IF cur_mthd = "AVG" THEN */

         if sct_mtl_tl <= 0
         then
            sct_mtl_tl = curcst.

         if sct_mtl_tl <> curcst
         then
            assign
               sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                           + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                           + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                           + sct_sub_ll
               sct_cst_date = today.
      end. /* IF sct_mtl_tl <> glxcst */
   end. /* IF qty_oh > 0 AND pct_po > 0 */
end. /* ELSE IF p-receipt-type = "" */

/* STORING VALUES FROM p_vph_cf_adj_amt & p_vph_cf_dscr_amt INTO */
/* l_vph_cf_adj_amt & l_vph_cf_dscr_amt RESPECTIVELY AND THEN    */
/* ROUNDING THESE VALUES SO THAT ROUNDED VALUES ARE PASSED AS    */
/* INPUT PARAMETERS FOR CALLING apvoglu1.p                       */

assign
   l_vph_cf_adj_amt  = p_vph_cf_adj_amt
   l_vph_cf_dscr_amt = p_vph_cf_dscr_amt.

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output l_vph_cf_adj_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output l_vph_cf_dscr_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

/* IF CONFIRMED COST ADJUSTMENT AMOUNT                      */
/* OR CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT          */
/* ARE DIFFERENT FROM VOUCHER,                              */
/*   CREATE IC-TYPE GL TRANSACTIONS ON INVENTORY DATABASE.  */

/* CREATE "IC" GL FOR CONFIRMED COST ADJUSTMENT */

if p_vph_adj_amt  <> 0
or p_vph_dscr_amt <> 0
then do:

   /* CHANGED SIXTEENTH INPUT PARAMETER FROM p_vph_cf_adj_amt TO    */
   /* l_vph_cf_adj_amt                                              */
   /* CHANGED SEVENTEENTH INPUT PARAMETER FROM p_vph_cf_dscr_amt TO */
   /* l_vph_cf_dscr_amt                                             */

   if glx_mthd = "AVG"
   then do:
      {gprun.i ""apvoglu1.p""
      "(input ""INV"",
        input ""ADD"",
        input p_ap_database,
        input p-po-site,
        input p_pvo_shipto,
        input p_vph_costadj_acct,
        input p_vph_costadj_sub,
        input p_vph_costadj_cc,
        input p_vph_dscr_acct,
        input p_vph_dscr_sub,
        input p_vph_dscr_cc,
        input p_pvo_project,
        input p_pvo_order,
        input p_vph_adj_amt,
        input p_vph_dscr_amt,
        input l_vph_cf_adj_amt,
        input l_vph_cf_dscr_amt,
        output l_gl_ref
        )"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* IF glx_mthd = "AVG" */

   /* STORE IC GL REF IN IC TRANSACTION IF AVAILABLE */
   /* THIS IS MORE USEFUL THAN THE VOUCHER NUMBER STORED EARLIER */
   if available trgl_det
   and l_gl_ref <> ""
   then
      trgl_gl_ref = l_gl_ref.

end. /* IF p_vph_adj_amt <> 0 */

if curflag
then do:
   {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
   for each spt_det
      exclusive-lock
      where spt_site = sct_site
        and spt_sim  = sct_sim
        and spt_part = sct_part:

      if spt_pct_ll = 1
      then
         assign
            spt_cst_tl = sct_mtl_tl
            spt_cst_ll = sct_mtl_ll.
      else
      if spt_pct_ll = 2
      then
         assign
            spt_cst_tl = sct_lbr_tl
            spt_cst_ll = sct_lbr_ll.
      else
      if spt_pct_ll = 3
      then
         assign
            spt_cst_tl = sct_bdn_tl
            spt_cst_ll = sct_bdn_ll.
      else
      if spt_pct_ll = 4
      then
         assign
            spt_cst_tl = sct_ovh_tl
            spt_cst_ll = sct_ovh_ll.
      else
      if spt_pct_ll = 5
      then
         assign
            spt_cst_tl = sct_sub_tl
            spt_cst_ll = sct_sub_ll.
      else
      /* DO NOT ZERO OUT COSTS FOR THE LOGISTICS CHARGE COST ELEMENT */
      if not can-find(first lc_mstr where lc_element = spt_element) then
         assign
            spt_cst_tl = 0
            spt_cst_ll = 0.
   end. /* FOR EACH spt_det */
end. /* IF curflag THEN DO */

if glxflag
then do:
   {gpsct01.i &set=glx_set &part=p_pvo_part &site=p_pvo_shipto}
   for each spt_det
      exclusive-lock
      where spt_site = sct_site
        and spt_sim  = sct_sim
        and spt_part = sct_part:

      if spt_pct_ll = 1
      then
         assign
            spt_cst_tl = sct_mtl_tl
            spt_cst_ll = sct_mtl_ll.
      else
      if spt_pct_ll = 2
      then
         assign
            spt_cst_tl = sct_lbr_tl
            spt_cst_ll = sct_lbr_ll.
      else
      if spt_pct_ll = 3
      then
         assign
            spt_cst_tl = sct_bdn_tl
            spt_cst_ll = sct_bdn_ll.
      else
      if spt_pct_ll = 4
      then
         assign
            spt_cst_tl = sct_ovh_tl
            spt_cst_ll = sct_ovh_ll.
      else
      if spt_pct_ll = 5
      then
         assign
            spt_cst_tl = sct_sub_tl
            spt_cst_ll = sct_sub_ll.
      else
         assign
            spt_cst_tl = 0
            spt_cst_ll = 0.
   end. /* FOR EACH spt_det ... */
end. /* IF glxflag */

/*------------------------------------------------------------------*/

PROCEDURE get_inv_and_discr_accts:

   define input parameter p_pvo_part          as character.
   define input parameter p_pvo_shipto          as character.
   define input parameter p_want_inv          as logical.
   define input parameter p_want_discr        as logical.
   define input-output parameter p_inv_acct   as character.
   define input-output parameter p_inv_sub    as character.
   define input-output parameter p_inv_cc     as character.
   define input-output parameter p_discr_acct as character.
   define input-output parameter p_discr_sub  as character.
   define input-output parameter p_discr_cc   as character.

   for first pt_mstr
      fields(pt_prod_line)
      where pt_part = p_pvo_part
      no-lock:

      for first pl_mstr
         fields(pl_inv_acct pl_inv_sub pl_inv_cc pl_dscr_acct pl_dscr_sub
                pl_dscr_cc)
         where pl_prod_line = pt_prod_line
         no-lock:
      end. /* FOR FIRST pl_mstr */

      for first pld_det
         fields(pld_inv_acct pld_inv_sub pld_inv_cc pld_dscracct pld_dscr_sub
                pld_dscr_cc)
         where pld_prodline = pt_prod_line
           and pld_site     = p_pvo_shipto
           and pld_loc      = ""
         no-lock:
      end. /* FOR FIRST pld_det */

      if not available pld_det
      then do:
         for first pld_det
            fields(pld_inv_acct pld_inv_sub pld_inv_cc pld_dscracct
                   pld_dscr_sub pld_dscr_cc)
            where pld_prodline = pt_prod_line
              and pld_site     = ""
              and pld_loc      = ""
            no-lock:
         end. /* FOR FIRST pld_det */
      end. /* IF NOT AVAILABLE pld_det */
   end. /* FOR FIRST pt_mstr */

   if available pld_det
   then do:
      if p_want_inv
      then
         assign
            p_inv_acct = pld_inv_acct
            p_inv_sub  = pld_inv_sub
            p_inv_cc   = pld_inv_cc.

      if p_want_discr
      then
         assign
            p_discr_acct = pld_dscracct
            p_discr_sub  = pld_dscr_sub
            p_discr_cc   = pld_dscr_cc.
   end. /* IF AVAILABLE pld_det THEN DO */
   else do:
      if p_want_inv
      then
         assign
            p_inv_acct = pl_inv_acct
            p_inv_sub  = pl_inv_sub
            p_inv_cc   = pl_inv_cc.
      if p_want_discr
      then
         assign
            p_discr_acct = pl_dscr_acct
            p_discr_sub  = pl_dscr_sub
            p_discr_cc   = pl_dscr_cc.
   end. /* ELSE DO */

END PROCEDURE. /* get_inv_and_discr_accts */
